let articleId;
let ajaxCount = 2;

$(() => {
    articleId = getQueryString("articleId");
    if (articleId !== "") {
        $.get(`/article/${articleId}`, (result, status) => {
            if (result.status === 200) {
                $('#title').text(result.data.title);
                $('#authorPortrait').attr("src", result.data.user.portrait);
                $('#authorNickName').text(result.data.user.nickName);
                $('#authorContent').html(result.data.content);
                afterFunc();
            } else if (result.status === 404) {
                $(location).attr('href', "/404.html");
            } else {
                alert(result);
            }
        });
        $.get(`/comment/${articleId}`, (result, status) => {
            if (result.status === 200) {
                appComment(result.data);
                afterFunc();
            } else {
                alert(result);
            }
        });
    } else {
        $(location).attr('href', "/404.html");
    }
});

function afterFunc() {
    ajaxCount--;
    if (ajaxCount === 0) {
        $('body').append(`
        <script src="/js/ckeditor.js"></script>
        <script src="/js/editor.js"></script>
        `);
    }

}

function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    return r != null ? unescape(r[2]) : "";
}

function commitArticle() {
    $.post("/comment/save", {
        'articleId': articleId,
        'content': myEditor.getData()
        }, function (data, status) {
            window.location.reload();
        }
    );
}

//拿云大人，他就是奸细
function whistleblowers() {
    $.post("/admin/reviewArticle/save", {
            'articleId': articleId
        }, function (data, status) {
        alert(data.status === 200 ? "举报成功" : "举报失败");
        }
    );
}
